#include "CameraDialog.h"
#include <QtGui>

CameraDialog::CameraDialog(Camera_Params &camParams, QWidget *parent) : QDialog(parent), camParams(camParams){
    this->setupUi(this);
    this->comboDeviceId->addItem("[Select a device]");
    QString path;
    validIp = false;
    for(int j=0; j<10; j++) {
        path = "/dev/video"+QString::number(j);
        QFile fout(path);
        if(fout.exists())
            this->comboDeviceId->addItem(path);
    }
}

CameraDialog::~CameraDialog()
{
}

void CameraDialog::on_buttonBox_rejected()
{
    this->hide();
}

void CameraDialog::on_buttonBox_accepted()
{
    if(this->radioLocal->isChecked())  {
        if(this->comboDeviceId->currentIndex()==0) {
            this->show();
            QMessageBox::information(this, "Information", "Please select a local camera device from the list");
        } else {
            camParams.deviceId = QString(this->comboDeviceId->currentText());
            camParams.option   = OPTION_LOCAL_CAMERA;
            emit data_ready();
        }
    } else {
        if(!validIp) {
            this->show();
            QMessageBox::information(this, "Information", "Please enter a valid IP");
        } else if(this->lineUser->text().length()==0) {
            this->show();
            QMessageBox::information(this, "Information", "Please enter a User name");
        } else if(this->linePassword->text().length()==0) {
            this->show();
            QMessageBox::information(this, "Information", "Please enter a User Password");
        } else if(this->linePort->text().length()==0) {
            this->show();
            QMessageBox::information(this, "Information", "Please enter a Port");
        } else {
            camParams.option = OPTION_IP_CAMERA;
            camParams.ipUser = this->lineUser->text();
            camParams.ipPass = this->linePassword->text();
            camParams.ipUrl  = this->lineIp->text();
            camParams.ipPort = this->linePort->text();
            emit data_ready();
        }
    }
}

void CameraDialog::on_lineIp_textChanged(const QString &arg1)
{
    int length = arg1.length(), valor=0;
    QString arg2 = arg1;
    for(int i=0; i<length; i++) {
        if(!(arg2.at(i).isNumber()||arg2.at(i)=='.'))
        {
            arg2.remove(length-1,1);
            this->lineIp->setText(arg2);
        }
    }

    QStringList bloqs;

    bloqs = arg2.split(".");

    QPalette *red = new QPalette();
    QPalette *green = new QPalette();
    red->setColor(QPalette::Text,Qt::red);
    green->setColor(QPalette::Text,Qt::green);

    if(bloqs.size()!=4){
        this->lineIp->setPalette(*red);
        validIp=false;
    } else{
        for(int i=0; i<4; i++)
        {
            if(bloqs.at(i).size()==0) {
                this->lineIp->setPalette(*red);
                validIp=false;
                break;
            }
            valor = bloqs.at(i).toInt();
            if(valor<256&&valor>=0) {
                this->lineIp->setPalette(*green);
                validIp=true;
            }
            else {
                this->lineIp->setPalette(*red);
                validIp=false;
            }

        }
    }


}

void CameraDialog::on_linePort_textChanged(const QString &arg1)
{
    int length = arg1.length();
    QString arg2 = arg1;

    for(int i=0; i<length; i++) {
        if(!(arg2.at(i).isNumber()))
        {
            arg2.remove(length-1,1);
            this->linePort->setText(arg2);
        }
    }
}
